package com.qn.visitor.service;

import com.qn.common.base.BaseServiceImpl;
import com.qn.visitor.entity.FeatureBlacklist;
import com.qn.visitor.mapper.FeatureBlacklistMapper;
import com.qn.visitor.model.bo.intercept.InterceptRule;
import com.qn.visitor.resources.config.FingerprinUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
 * qingniu-cloud user source analysis
 *
 * @Author: TXG
 * @Date: 2024/6/11
 * @Version V0.0.2
 */
@DubboService(version = "1.0.0")
public class FeatureBlacklistServiceImpl
        extends BaseServiceImpl<FeatureBlacklist>
        implements FeatureBlacklistService {
    @Autowired
    FeatureBlacklistMapper baseMapper;
    @Override
    public List<FeatureBlacklist> getFeatureBlacklist() {
        return baseMapper.getFeatureBlacklist();
    }
    @Override
    public InterceptRule getBlackRule() {
        //TODO:此处需要添加缓存
        List<FeatureBlacklist> blacklists = baseMapper.getFeatureBlacklist();
        if (blacklists == null || blacklists.size() < 1) {
            return null;
        }
        InterceptRule rule = new InterceptRule();
        Map<String, List<FeatureBlacklist>> blackMap = blacklists.stream()
                .collect(Collectors.groupingBy(FeatureBlacklist::getFeatureType));
        for (String key : blackMap.keySet()) {
            List<FeatureBlacklist> blacks = blackMap.get(key);
            if (blacklists != null && blacklists.size() > 0) {
                List<String> features = blacks.stream().map(FeatureBlacklist::getFeature).toList();
                if (FingerprinUtils.CKF.equals(key)) {
                    rule.setCkfBlacklist(features);
                } else if (FingerprinUtils.IP4.equals(key)) {
                    rule.setIp4Blacklist(features);
                } else if (FingerprinUtils.JA4.equals(key)) {
                    rule.setJa4Blacklist(features);
                }
            }
        }
        return rule;
    }
}
